<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="100px">
      <el-form-item label="制造商名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入制造商名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="官网地址" prop="url">
        <el-input
          v-model="queryParams.url"
          placeholder="请输入网站URL"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="技术支持网站" prop="support_url">
        <el-input
          v-model="queryParams.support_url"
          placeholder="请输入技术支持网站URL"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="技术支持电话" prop="support_phone">
        <el-input
          v-model="queryParams.support_phone"
          placeholder="请输入技术支持电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="技术支持邮箱" prop="support_email">
        <el-input
          v-model="queryParams.support_email"
          placeholder="请输入技术支持邮箱"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['vadmin/basic:manufacturers:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['vadmin/basic:manufacturers:edit']"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['vadmin/basic:manufacturers:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['admin:assets:assets:export']"
          type="primary"
          plain
          icon="el-icon-position"
          size="mini"
          :loading="exportLoading"
          @click="handleInput"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['admin:basic:manufacturers:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="manufacturersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="制造商名称" align="center" prop="name" width="200"  sortable />
      <el-table-column label="网站官网" align="center" prop="url" width="200"  sortable />
      <el-table-column label="技术支持官网" align="center" prop="support_url" width="200"  sortable />
      <el-table-column label="技术支持电话" align="center" prop="support_phone" width="200"  sortable />
      <el-table-column label="技术支持邮箱" align="center" prop="support_email" width="200"  sortable />
      <el-table-column label="图片" align="center" prop="image" >
        <div slot-scope="scope">
          <img v-if="scope.row.image" :src="scope.row.image" style="width: 50px;height: 50px" />
        </div>>
      </el-table-column>
      <el-table-column label="操作" width="155" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['vadmin/basic:manufacturers:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['vadmin/basic:manufacturers:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改制造商对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="制造商名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入制造商名称" />
        </el-form-item>
        <el-form-item label="网站官网" prop="url">
          <el-input v-model="form.url" placeholder="请输入网站官网URL" />
        </el-form-item>
        <el-form-item label="技术支持官网" prop="support_url">
          <el-input v-model="form.support_url" placeholder="请输入技术支持官网URL" />
        </el-form-item>
        <el-form-item label="技术支持电话" prop="support_phone">
          <el-input v-model="form.support_phone" placeholder="请输入技术支持电话" />
        </el-form-item>
        <el-form-item label="技术支持邮箱" prop="support_email">
          <el-input v-model="form.support_email" placeholder="请输入技术支持邮箱" />
        </el-form-item>
        <el-form-item label="图片">
          <el-button
            type="primary"
            plain
            icon="el-icon-upload"
            size="mini"
            @click="imageUpload"
          >图片上传
          </el-button>
          <img v-if="form.image" :src="form.image" class="avatar">
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 图片上传 -->
    <el-dialog :title="title1" :visible.sync="open1" width="400px" append-to-body @close="open1 = false">
      <FileUpload ref="saveFile" :file-type="['ALL']" @input="fileGet" />
    </el-dialog>
    <InputFile ref="inputfile" @inputFile="inputFile"></InputFile>
  </div>
</template>

<script>
// eslint-disable-next-line no-unused-vars
import {
  listManufacturers,
  getManufacturers,
  delManufacturers,
  addManufacturers,
  updateManufacturers,
  exportManufacturers,
  importsManufacturers
} from "@/api/vadmin/basic/manufacturers";
import FileUpload from "@/components/FileUpload/index";
import InputFile from "@/views/components/InputFile";

export default {
  name: "Manufacturers",
  components: { InputFile, FileUpload },
  data() {
    return {
      title1: "",
      open1: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 制造商表格数据
      manufacturersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        description: null,
        modifier: null,
        dept_belong_id: null,
        update_datetime: null,
        create_datetime: null,
        name: null,
        deleted_datetime: null,
        url: null,
        support_url: null,
        support_phone: null,
        support_email: null,
        image: null,
        creator_id: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "制造商名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleInput() {
      this.$refs.inputfile.open();
    },
    inputFile(val) {
      // 是否更新已经存在的用户数据
      importsManufacturers(val).then(response => {
        this.$alert("导入成功！", "导入结果", { dangerouslyUseHTMLString: true });
        this.getList();
      });
    },
    fileGet(file) {
      this.open1 = false;
      this.$refs.saveFile.fileList = [];
      this.form.image = file;
    },
    imageUpload() {
      this.open1 = true;
      this.title1 = "图片上传";
    },
    /** 查询制造商列表 */
    getList() {
      this.loading = true;
      listManufacturers(this.queryParams).then(response => {
        this.manufacturersList = response.data.results;
        this.total = response.data.count;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        description: null,
        modifier: null,
        dept_belong_id: null,
        update_datetime: null,
        create_datetime: null,
        name: null,
        deleted_datetime: null,
        url: null,
        support_url: null,
        support_phone: null,
        support_email: null,
        image: null,
        creator_id: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加制造商";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getManufacturers(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改制造商";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateManufacturers(this.form).then(response => {
              this.$message({
                message: "修改成功",
                type: "success"
              });
              this.open = false;
              this.getList();
            });
          } else {
            addManufacturers(this.form).then(response => {
              this.$message({
                message: "新增成功",
                type: "success"
              });
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        delManufacturers(ids).then(res => {
          this.getList();
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        });
      }).catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除"
        });
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有数据?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return exportManufacturers(queryParams);
      }).then(response => {
        this.download(response.data.file_url, response.data.name);
      });
    }
  }
};
</script>
<style>
.avatar {
  width: 178px;
  height: 178px;
  display: block;
  margin-top: 15px;
}
</style>
